package com.example.emqxclient;

import com.example.emqxclient.client.EmqClient;
import com.example.emqxclient.properties.MqttProperties;
import com.example.emqxclient.properties.QosEnum;
import com.example.emqxclient.properties.TestMqttProperties;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
//import org.springframework.cloud.openfeign.EnableFeignClients;


// Generated by https://start.springboot.io
// 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn
@SpringBootApplication
@Slf4j
@EnableFeignClients
@EnableDiscoveryClient //A的启动类上添加了 @EnableDiscoveryClient 注解来启用 Nacos 服务注册功能
public class EmqxClientApplication implements ApplicationRunner {
    @Resource
    private EmqClient emqClient;
    @Resource
    private MqttProperties mqttProperties;
    @Resource
    private TestMqttProperties testMqttProperties;

    public static void main(String[] args) {
        SpringApplication.run(EmqxClientApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("testMqttProperties:"+testMqttProperties.toString());

        emqClient.connect(mqttProperties.getUsername(), mqttProperties.getPassword());
        emqClient.subscribe(mqttProperties.getAcceptTopic(), QosEnum.QoS2);
//        new Thread() {
//            @Override
//            public void run() {
//                super.run();
//                while (true) {
//                    try {
//                        sleep(5000);
//                        String msg = "msg " + new Date();
//                        emqClient.publish("testtopic/A", msg, QosEnum.QoS2, false);
//
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//
//            }
//        }.start();
    }
}
